﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Web;
using System.Collections;

namespace SJHT_CustomerVoiceDownLoad.Common
{
    /// <summary>
    /// 日志文件类
    /// </summary>
    public abstract class LogFile
    {
        private static Dictionary<String, int> filecount = new Dictionary<string, int>();
        /// <summary>
        /// 日志根目录
        /// </summary>
        private static string _FileDirectory = "Log/";
        /// <summary>
        /// 默认日志文件名
        /// </summary>
        private static string _FileName = "Log";
        /// <summary>
        /// 默认错误日志文件名
        /// </summary>
        private static string _ErrFileName = "Err";

        /// <summary>
        /// 是否输出日志
        /// </summary>
        public enum FileType
        {
            TXT
        }
        /// <summary>
        /// 获取文件后缀字符串
        /// </summary>
        /// <returns></returns>
        private static String GetFileTypeString(FileType type)
        {
            switch (type)
            {
                case FileType.TXT:
                    return ".txt";
                default:
                    return ".txt";
            }
        }


        /// <summary>
        /// 创建、获取日志文件
        /// </summary>
        /// <param name="filePath">文件路径 sys/</param>
        /// <param name="fileName">文件名 init</param>
        /// <param name="type">文件类型</param>
        /// <returns></returns>
        private static Stream GetFileStream(String filePath, String fileName, FileType type)
        {
            //文件目录
            String FileDirectory = _FileDirectory + DateTime.Now.ToString("yyyyMMdd") + "/" + filePath;
            //判断文件目录是否存在
            if (!Directory.Exists(FileDirectory))
            {
                //创建文件目录
                Directory.CreateDirectory(FileDirectory);
            }

            //文件名
            String FileName = fileName + DateTime.Now.ToString("yyyyMMdd");
            if (!filecount.ContainsKey(FileName))
            {
                filecount.Add(FileName, 1);
            }

            //文件全路径名
            String FileNameFull = FileDirectory + FileName + "_" + filecount[FileName] + GetFileTypeString(type);

            FileStream stream = null;
            //判断是否存在文件
            if (File.Exists(FileNameFull))
            {
                stream = new FileStream(FileNameFull, FileMode.Append);
                //文件超过大小
                if (stream.Length > 2 * 1024 * 1024)
                {
                    stream.Flush();
                    stream.Close();
                    int count = filecount[FileName] + 1;
                    filecount.Remove(FileName);
                    filecount.Add(FileName, count);
                    stream = GetFileStream(filePath, fileName, type) as FileStream;
                }
            }
            else
            {
                stream = new FileStream(FileNameFull, FileMode.Create);
            }
            return stream;

        }

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="filePath">文件路径 sys/</param>
        /// <param name="fileName">文件名 init</param>
        /// <param name="type">文件类型</param>
        private static void WriteLogFile(String filePath, String fileName, FileType type, String content)
        {
            Stream stream = LogFile.GetFileStream(filePath, fileName, type);
            using (StreamWriter sw = new StreamWriter(stream))
            {
                String time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                sw.WriteLine("[" + time + "]" + content);
                sw.Flush();
            }
            stream.Close();
        }
        /// <summary>
        /// 自动写错误日志文件
        /// </summary>
        /// <param name="e"></param>
        public static void WriteException(Exception e)
        {
            if (e != null)
            {

                LogFile.WriteLogFile("", _ErrFileName + "_", FileType.TXT, "【异常类型】" + e.GetType().Name);
                LogFile.WriteLogFile("", _ErrFileName + "_", FileType.TXT, "【异常信息】" + e.Message);
                LogFile.WriteLogFile("", _ErrFileName + "_", FileType.TXT, "【堆栈调用】" + e.StackTrace);
                LogFile.WriteLogFile("", _ErrFileName + "_", FileType.TXT, "==========================================================");
            }
            else
            {
                LogFile.WriteLogFile("", _ErrFileName + "_", FileType.TXT, "【未处理异常】" + e.ToString());
            }
        }
        /// <summary>
        /// 自动写入错误日志文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="e"></param>
        public static void WriteException(String fileName, Exception e)
        {
            if (e != null)
            {
                LogFile.WriteLogFile("", fileName + "_", FileType.TXT, "【异常类型】" + e.GetType().Name);
                LogFile.WriteLogFile("", fileName + "_", FileType.TXT, "【异常信息】" + e.Message);
                LogFile.WriteLogFile("", fileName + "_", FileType.TXT, "【堆栈调用】" + e.StackTrace);
                LogFile.WriteLogFile("", fileName + "_", FileType.TXT, "==========================================================");
            }
            else
            {
                LogFile.WriteLogFile("", fileName + "_", FileType.TXT, "【未处理异常】" + e.ToString());
            }
        }
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="content">内容</param>
        public static void Write(String content)
        {
            LogFile.WriteLogFile("", _FileName + "_", FileType.TXT, content);
        }
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="content">内容</param>
        public static void Write(String fileName, String content)
        {
            LogFile.WriteLogFile("", fileName + "_", FileType.TXT, content);
        }
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="filePath">文件路径 sys/</param>
        /// <param name="fileName">文件名</param>
        /// <param name="content">内容</param>
        public static void Write(String filePath, String fileName, String content)
        {
            LogFile.WriteLogFile(filePath, fileName + "_", FileType.TXT, content);
        }


    }
}
